const path = require('path')
const argv = require('yargs').argv
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const WebpackBuildNotifierPlugin = require('webpack-build-notifier')
const Pages = require('./pages.config')
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
let entry = {}
let html = []
Pages.filter(page => page.publish).forEach(page => {
  page.url = page.url ? page.url + '/' : page.url
  entry[`${page.url}index`] = `./src/${page.url}index.js`
  html.push(
    new HtmlWebpackPlugin({
      template: `./src/${page.url}index.html`,
      filename: `${page.url}index.html`,
      favicon: `./src/${page.url}/favicon.ico`,
      chunks: [`${page.url}index`],
      hash: true,
      cache: false
    })
  )
})
// 生产下 移动文件
const moveFile =
  argv.mode !== 'development'
    ? [
        new CopyWebpackPlugin(
          [
            { from: 'src/asset/uedit', to: 'asset/uedit' },
            { from: 'src/asset/img', to: 'asset/img' }
          ],
          {}
        )
      ]
    : []

console.log('##########')
// console.log(argv.mode)
console.log('请访问：' + argv.host + ':' + argv.port)
console.log('##########')

module.exports = {
  devServer: {
    //设置多个服务目录
    contentBase: [path.join(__dirname, 'src')],
    historyApiFallback: true,
    disableHostCheck: true,
    hot: true
  },
  entry,
  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/', //argv.mode === 'development' ? '' : './',
    filename: 'asset/[name]-[hash:5].js'
  },
  module: {
    rules: [
      // 处理js
      // {
      //   test: /\.js$/,
      //   exclude: /node_modules/,
      //   loader: "eslint-loader",
      //   options: {
      //     formatter: require("eslint-friendly-formatter"),
      //   }
      // },
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              cacheDirectory: true,
              presets: ['react', 'stage-0'],
              plugins: [
                'transform-decorators-legacy',
                'transform-runtime',
                ['import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css' }]
              ]
            }
          },
          {
            loader: 'eslint-loader'
          }
        ]
      },
      // 处理样式文件
      {
        test: /\.(css|scss)$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              minimize: argv.mode !== 'development'
            }
          },
          {
            loader: 'postcss-loader'
          },
          {
            loader: 'sass-loader'
          }
        ]
      },
      // 处理html
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
            options: {
              root: path.resolve(__dirname, 'assets')
            }
          }
        ]
      },
      // 处理其他资源
      {
        test: /\.(png|jpg|jpeg|gif)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 1024 * 8,
              name: '[path][name].[hash:5].[ext]',
              context: './src'
            }
          }
        ]
      }
    ]
  },
  plugins: [
    ...html,
    ...moveFile,
    new MiniCssExtractPlugin({
      filename: 'asset/[name].css'
    }),
    new webpack.HotModuleReplacementPlugin({}),
    new WebpackBuildNotifierPlugin({
      title: '溢米ERP项目',
      suppressSuccess: true,
      suppressCompileStart: true,
      suppressWarning: true,
      activateTerminalOnError: true
    })
  ]
}
